package trans.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import trans.action.support.dto.Attachment_Json;
import trans.action.support.dto.UserTips_Json;
import trans.action.support.dto.User_Json;
import trans.model.SaAttachment;
import trans.model.User;
import trans.model.UserTips;
import trans.service.AttachmentDAO;
import trans.service.UserDAO;
import trans.support.BasicSupport;
import trans.support.Constants;

public class JsonAction extends BasicSupport{

	private static final long serialVersionUID = 1L;
	
	Integer userId; 
	String clientName;
	public String getClientName() throws Exception{
		User user =  UserDAO.getUser(userId);
		
		User_Json uj = new User_Json();
		uj.setClientName(user.getUserCompName());
		writeJsonObj(uj);

		return null;
	}
	
	List<UserTips> userTips = new ArrayList<UserTips>();
	public String getUserTips() throws Exception{
		userTips = UserDAO.getUserTips(userId);
		
		List<Object> result = new ArrayList<Object>();
		for(UserTips each : userTips){
			UserTips_Json utj = new UserTips_Json();
			utj.setTipType(each.getTipType());
			utj.setTipValue(each.getTipValue());
			result.add(utj);
		}
		writeJsonList(result);
		return null;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public void setUserTips(List<UserTips> userTips) {
		this.userTips = userTips;
	}
	
	Integer saId;
	String saType;
	String state;
	public String getSaAttachments() throws Exception{
		int stateValue = 0;
		Map<Integer,String> states = null;
		if(Constants.SATYPE_AIR.equals(saType)){
			states = Constants.AIRSA_STATES;
		}else if(Constants.SATYPE_OCEAN.equals(saType)){
			states = Constants.OCEANSA_STATES;
		}else{
			return null;
		}
		for(Integer value : states.keySet()){
			if(states.get(value).equals(state)){
				stateValue = value;
			}
		}
		
		List<SaAttachment> attachments = 
			AttachmentDAO.getAllAttachments(saId, stateValue,currentUser().getUserRole());
		
		
		List<Object> result = new ArrayList<Object>();
		for(SaAttachment each : attachments){
			Attachment_Json atj = new Attachment_Json();
			atj.setFileName(each.getFileName());
			atj.setAttachId(each.getId());
			result.add(atj);
		}
		writeJsonList(result);
		return null; 
	}
	public Integer getSaId() {
		return saId;
	}
	public void setSaId(Integer saId) {
		this.saId = saId;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getSaType() {
		return saType;
	}
	public void setSaType(String saType) {
		this.saType = saType;
	}
}
